using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL {
/// <summary>
/// The TownshipCodeTableDB class is responsible for interacting with the database to retrieve and store information
/// about TownshipCodeTable objects.
/// </summary>
public class TownshipCodeTableDB

{
#region "Public Methods"

/// <summary>
/// Gets an instance of TownshipCodeTable from the underlying datasource.
/// </summary>
/// <param name="townshipCode">The unique TownshipCode of the TownshipCodeTable in the database.</param>
/// <returns>An TownshipCodeTable when the TownshipCode was found in the database, or null otherwise.</returns>
public static TownshipCodeTable GetItem(short townshipCode)
{
TownshipCodeTable myTownshipCodeTable = null;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTownshipCodeTableSelectSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@townshipCode", townshipCode);

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.Read()) {
myTownshipCodeTable = FillDataRecord(myReader);
}
myReader.Close();
}
myConnection.Close();
}
return myTownshipCodeTable;
}
}

/// <summary>
/// Returns a list with TownshipCodeTable objects.
/// </summary>
/// <returns>A generics List with the TownshipCodeTable objects.</returns>
public static TownshipCodeTableList GetList()
{
TownshipCodeTableList tempList = new TownshipCodeTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTownshipCodeTableSelectList", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
}
return tempList;
}

/// <summary>
/// Saves a TownshipCodeTable in the database.
/// </summary>
/// <param name="myTownshipCodeTable">The TownshipCodeTable instance to save.</param>
/// <returns>The new TownshipCode if the TownshipCodeTable is new in the database or the existing TownshipCode when an item was updated.</returns>
public static Int16 Save(TownshipCodeTable myTownshipCodeTable)
{
Int16 result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTownshipCodeTableInsertUpdateSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

if (myTownshipCodeTable.TownshipCode == -1)
{myCommand.Parameters.AddWithValue("@townshipCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@townshipCode", myTownshipCodeTable.TownshipCode);
}
if (myTownshipCodeTable.CityCode == null){
myCommand.Parameters.AddWithValue("@cityCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@cityCode", myTownshipCodeTable.CityCode);
}
if (myTownshipCodeTable.DistrictCode == null){
myCommand.Parameters.AddWithValue("@districtCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@districtCode", myTownshipCodeTable.DistrictCode);
}
if (myTownshipCodeTable.CountryCode == null){
myCommand.Parameters.AddWithValue("@countryCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@countryCode", myTownshipCodeTable.CountryCode);
}
if (string.IsNullOrEmpty(myTownshipCodeTable.TownshipName))
{
myCommand.Parameters.AddWithValue("@townshipName", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@townshipName", myTownshipCodeTable.TownshipName);
}
if (string.IsNullOrEmpty(myTownshipCodeTable.TownshipShortName))
{
myCommand.Parameters.AddWithValue("@townshipShortName", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@townshipShortName", myTownshipCodeTable.TownshipShortName);
}
if (string.IsNullOrEmpty(myTownshipCodeTable.TownshipOtherName))
{
myCommand.Parameters.AddWithValue("@townshipOtherName", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@townshipOtherName", myTownshipCodeTable.TownshipOtherName);
}
if (string.IsNullOrEmpty(myTownshipCodeTable.Remark))
{
myCommand.Parameters.AddWithValue("@remark", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@remark", myTownshipCodeTable.Remark);
}
if (myTownshipCodeTable.Status == null){
myCommand.Parameters.AddWithValue("@status", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@status", myTownshipCodeTable.Status);
}
if (myTownshipCodeTable.CreatedDateTime == null){
myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@createdDateTime", myTownshipCodeTable.CreatedDateTime);
}
if (myTownshipCodeTable.ModifiedDateTime == null){
myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@modifiedDateTime", myTownshipCodeTable.ModifiedDateTime);
}

DbParameter returnValue;
returnValue = myCommand.CreateParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(returnValue);

myConnection.Open();
myCommand.ExecuteNonQuery();
result = Convert.ToInt16(returnValue.Value);
myConnection.Close();
}
}
return result;
}

/// <summary>
/// Deletes a TownshipCodeTable from the database.
/// </summary>
/// <param name="townshipCode">The TownshipCode of the TownshipCodeTable to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
public static bool Delete(short townshipCode)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTownshipCodeTableDeleteSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue("@townshipCode", townshipCode);
myConnection.Open();
result = myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
return result > 0;
}

#endregion

/// <summary>
/// Initializes a new instance of the TownshipCodeTable class and fills it with the data fom the IDataRecord.
/// </summary>
private static TownshipCodeTable FillDataRecord(IDataRecord myDataRecord )
{
TownshipCodeTable myTownshipCodeTable = new TownshipCodeTable();
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("TownshipCode")))
{
myTownshipCodeTable.TownshipCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("TownshipCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CityCode")))
{
myTownshipCodeTable.CityCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("CityCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("DistrictCode")))
{
myTownshipCodeTable.DistrictCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("DistrictCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CountryCode")))
{
myTownshipCodeTable.CountryCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("CountryCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("TownshipName")))
{
myTownshipCodeTable.TownshipName = myDataRecord.GetString(myDataRecord.GetOrdinal("TownshipName"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("TownshipShortName")))
{
myTownshipCodeTable.TownshipShortName = myDataRecord.GetString(myDataRecord.GetOrdinal("TownshipShortName"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("TownshipOtherName")))
{
myTownshipCodeTable.TownshipOtherName = myDataRecord.GetString(myDataRecord.GetOrdinal("TownshipOtherName"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Remark")))
{
myTownshipCodeTable.Remark = myDataRecord.GetString(myDataRecord.GetOrdinal("Remark"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
{
myTownshipCodeTable.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
{
myTownshipCodeTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
{
myTownshipCodeTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
}
return myTownshipCodeTable;
}
}

 } 
